import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/base_widget/base_image.dart';
import 'package:jetour_line/base_widget/my_alert.dart';
import 'package:jetour_line/base_widget/sel_btn_widget.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/utils/extension_int.dart';

import '../../model/common_list_title_model.dart';
import '../../network/jetour_http.dart';

class EndTravelTypeWidget extends StatefulWidget {
  final Function cateCallBack;
  int cate;
  EndTravelTypeWidget({super.key, required this.cateCallBack,this.cate = 0});

  @override
  State<EndTravelTypeWidget> createState() => _EndTravelTypeWidgetState();
}

class _EndTravelTypeWidgetState extends State<EndTravelTypeWidget> {
  List<String> types = [];
  List<CommonTitleListModel> list = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    JetourHttp().get(JetourRouter.GuideCateList).then((result) {
      for (int i = 0; i < result.length; i++) {
        CommonTitleListModel model =  CommonTitleListModel.fromJson(result[i]);
        list.add(CommonTitleListModel.fromJson(result[i]));
        if(widget.cate >0){
          if(widget.cate == model.id){
            selIndex = i;
            type = model.name;
          }
        }
        types.add(model.name);
      }
      setState(() {

      });
    }).catchError((e) {});
  }


  String? type;
  int? typeId;
  int selIndex = -1;

  @override
  Widget build(BuildContext context) {
    return types.isEmpty?const SizedBox(): GestureDetector(
      onTap: () {
        showBottomAlert(
            context,
            SelBtnWidget(
                title: "选择分类",
                selIndex: selIndex,
                callBack: (int index){
                  type = list[index].name;
                  typeId = list[index].id;
                  widget.cateCallBack(typeId);
                  selIndex = index;
                  setState(() {

                  });
                },
                msg: types));
      },
      child: Container(
        padding: EdgeInsets.only(left: 32.px, right: 32.px),
        alignment: Alignment.centerLeft,
        color: Colors.transparent,
        width: double.infinity,
        height: 94.px,
        child: Row(
          children: [
            Expanded(
                child: Text(
              '选择分类',
              style: TextStyle(
                  fontSize: 32.px,
                  color: MyColors.color1A1A1A,
                  fontWeight: FontWeight.w600),
            )),
            Text(
              type ?? '请选择',
              style: TextStyle(
                fontSize: 30.px,
                color: MyColors.color999999,
              ),
            ),
            SizedBox(
              width: 24.px,
            ),
            BaseImage(
              image: 'btn_jump',
              width: 40.px,
              height: 40.px,
            ),
          ],
        ),
      ),
    );
  }
}
